#include "SpriteLoader.hpp"

#include <sstream>

using namespace TheEmpires;

SpriteLoader::SpriteLoader()
{

}

SpriteLoader::~SpriteLoader()
{
	DestroySprites();
}

void SpriteLoader::DestroySprites()
{
	for(SpriteMap::iterator it = sprites.begin(); it != sprites.end(); it++)
	{
		delete it->second;
	}

	sprites.clear();
}

int SpriteLoader::LoadSprites(std::string path)
{
	int id					= 1;

	for(; ; id++)
	{
		std::stringstream filename;
		Image* image			= new Image; // Memory leak but what the fuck

		filename << path << id << ".png";

		if(!image->LoadFromFile(filename.str()))
		{
			break;
		}

		sprites[id]			= new Sprite(*image);
	}

	// Return the amount of sprites loaded
	return (id - 1);
}

sf::Sprite* SpriteLoader::GetSprite(uint32_t id)
{
	SpriteMap::iterator it		= sprites.find(id);

	if(it != sprites.end())
	{
		return it->second;
	}

	return NULL;
}
